package jp.co.ricoh.melody.views.fragments;

import android.app.Dialog;
import android.databinding.DataBindingUtil;
import android.os.Bundle;
import android.support.annotation.NonNull;
import android.support.annotation.Nullable;
import android.support.v4.app.DialogFragment;
import android.text.TextUtils;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.view.Window;

import jp.co.ricoh.melody.R;
import jp.co.ricoh.melody.databinding.AlarmDialogFragmentBinding;
import jp.co.ricoh.melody.models.entities.AlarmData;

/**
 * Copyright (C) 2016 RICOH Co.,LTD.
 * All rights reserved.
 */
public class AlarmDialogFragment extends DialogFragment {

    private static final String EXTRA_ALARM_DATA = "melody";
    private static final String TAG = AlarmDialogFragment.class.getSimpleName();
    private AlarmDialogFragmentBinding mBinding;

    /**
     * Callback when dismiss
     */
    public interface OnDismissListener {
        void onDismissFromUser();
    }

    private OnDismissListener mOnDismissListener;

    public void setOnDismissListener(OnDismissListener onDismissListener) {
        mOnDismissListener = onDismissListener;
    }

    @Nullable
    @Override
    public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
        mBinding = DataBindingUtil.inflate(inflater, R.layout.fragment_dialog_alarm, container, false);
        mBinding.txtDismiss
                .setOnClickListener(new View.OnClickListener() {
                    @Override
                    public void onClick(View view) {
                        if (mOnDismissListener != null) {
                            mOnDismissListener.onDismissFromUser();
                        }
                    }
                });
        setAlarmData((AlarmData) getArguments().getSerializable(EXTRA_ALARM_DATA));
        setCancelable(false);
        return mBinding.getRoot();
    }

    private void setAlarmData(AlarmData alarmData) {
        if (alarmData == null) {
            throw new IllegalArgumentException("AlarmData must not null");
        }
        if (TextUtils.isEmpty(alarmData.getLabel())) {
            mBinding.txtLabel.setVisibility(View.INVISIBLE);
        } else {
            mBinding.txtLabel.setText(alarmData.getLabel());
        }
    }

    @NonNull
    @Override
    public Dialog onCreateDialog(Bundle savedInstanceState) {
        Dialog dialog = super.onCreateDialog(savedInstanceState);
        dialog.requestWindowFeature(Window.FEATURE_NO_TITLE);
        dialog.setCanceledOnTouchOutside(false);
        return dialog;
    }

    @Override
    public void onStart() {
        super.onStart();
        Dialog dialog = getDialog();
        dialog.getWindow().setLayout(getContext().getResources().getDimensionPixelSize(R.dimen.dimen_dialog_alarm_width),
                getContext().getResources().getDimensionPixelSize(R.dimen.dimen_dialog_alarm_height));

    }

    /**
     * Initialize a new fragment with the given melody
     *
     * @param alarmData
     * @return
     */
    public static AlarmDialogFragment newInstance(AlarmData alarmData) {
        AlarmDialogFragment fragment = new AlarmDialogFragment();
        Bundle args = new Bundle();
        args.putSerializable(EXTRA_ALARM_DATA, alarmData);
        fragment.setArguments(args);
        return fragment;
    }

}
